<template>
	<div class="edit-h5">
		<u-navbar placeholder :title="TUITranslateService.t(`TUIGroup.发布群公告`)" rightText="发布"
			@rightClick="updateProfile" autoBack>
		</u-navbar>
		<div class="notification-box">
			<u-textarea v-model="notification" maxlength="100" placeholder="请输入内容" count height="600rpx"></u-textarea>
		</div>

	</div>
</template>

<script lang="ts" setup>
	import { nextTick, reactive } from "../../../adapter-vue";
	import {
		TUITranslateService,
		IGroupModel,
		TUIStore,
		StoreName,
		TUIGroupService
	} from "@tencentcloud/chat-uikit-engine";
	import { Toast, TOAST_TYPE } from "../../common/Toast/index";
	import { watchEffect, ref } from "../../../adapter-vue";
	import { onLoad, onShow } from "@dcloudio/uni-app";
	import { checkUserInfo } from "../../../utils/checkLogin";
	const groupID = ref('')
	const notification = ref('');
	const userInfo = ref<any>({})
	onLoad((options : any) => {
		groupID.value = options.groupID
	})
	onShow(() => {
		TUIStore.watch(StoreName.CUSTOM, {
			IMuserInfo: (data : any) => {
				checkUserInfo()
				if (data) {
					userInfo.value = data
					TUIGroupService.getGroupProfile({
						groupID: groupID.value,
					}).then((res) => {
						if (!!res.data.group.notification) {
							let notificationInfo = res.data.group.notification.split('&notificationOpt=')
							notification.value = notificationInfo[0]
						}

					}).catch((error) => {
						console.log(error, "异常")
					})
				}

			}
		})
	})
	//发布群公告
	const updateProfile = () => {
		if (notification.value === '') {
			return Toast({
				message: '请输入群公告',
				type: 'none'
			})
		}

		let notificationOpt = {
			date: uni.$u.timeFormat(new Date().getTime(), 'yyyy-mm-dd hh:MM:ss'),
			name: userInfo.value.nick
		}
		TUIGroupService.updateGroupProfile({
			groupID: groupID.value,
			notification: notification.value + '&notificationOpt=' + JSON.stringify(notificationOpt),
		}).then(res => {
			let currentGroup = res.data.group;
			TUIStore.update(StoreName.GRP, 'currentGroup', currentGroup)
			uni.navigateBack()
		})
	}
</script>

<style lang="scss" scoped>
	::v-deep .u-navbar__content__right {
		color: #ff0012;
	}

	.notification-box {
		padding: 24rpx;
	}
</style>